package com.mall.task.psvideo;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class GetMessageFromYiDian {

	public static List<String> urlList = null;

	public static void main(String[] args) throws IOException, Exception {

		Connection conn = null;
		String dbUrl = "jdbc:mysql://192.168.31.187:3306/mall_b2b?"
				+ "user=root&password=123456&useUnicode=true&characterEncoding=UTF8";
		Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动

		conn = (Connection) DriverManager.getConnection(dbUrl);
		Statement stmt = (Statement) conn.createStatement();
		urlList = new ArrayList<>();
		for (int i = 1; i < 15; i++) {
			String url = "http://www.yidian51.com/tmall/f6-p" + i;
			Document docCompanylDocument = Jsoup.connect(url).get();

			Elements elements = docCompanylDocument.getElementsByClass("bigtit");

			for (int j = 0; j < elements.size(); j++) {

				if (urlList.contains(elements.get(j).getElementsByTag("a").attr("href"))) {
					continue;
				}
				urlList.add(elements.get(j).getElementsByTag("a").attr("href"));
			}
		}
		System.out.println(urlList.size());
		for (int k = 0; k < urlList.size(); k++) {
			String detailUrl = urlList.get(k);
			Document docCompanyDetailDocument = Jsoup.connect(detailUrl).get();
			String companyName = docCompanyDetailDocument.getElementsByClass("bigtit").get(0).text();// 店铺名字
			String price1 = docCompanyDetailDocument.getElementsByClass("price1").get(0).text();
			String saleprice = price1.split("￥")[1];// 出售的价格

			Elements xiaobao = docCompanyDetailDocument.getElementsByClass("xiaobao").select("strong");
			String jishunianfei = xiaobao.get(0).text();// 技术年费
			String xfzbzj = xiaobao.get(1).text();// 消费者保证金

			String bianhao = docCompanyDetailDocument.getElementsByClass("bianhao").select("p").get(0).text().trim();
			String wangdianNum = bianhao.split("号")[1].replace("     ", "");// 网店编号
			String maijiadesc = docCompanyDetailDocument.getElementsByClass("p2").get(2).text();// 卖家店铺描述
			String zhuangtai = docCompanyDetailDocument.getElementsByClass("bianhao").select("p").select("i").get(0)
					.text();

			// System.out.println(status);

			Elements list = docCompanyDetailDocument.getElementsByClass("content_detail").select("li").select("span");
			String comtype = list.get(0).text();// 网店类型
			String shangchengtype = list.get(1).text();// 商城类型
			String shangchengCategory = list.get(2).text();// 商城类目
			String renqi = list.get(3).text();// 人气
			String ruzhuTime = list.get(4).text();// 入住时间
			String daikuan = list.get(5).text();// 有无贷款
			String daihuo = list.get(6).text();/// 是否带货
			String area = list.get(7).text();// 所在地区
			String yibanweigui = list.get(8).text();// 一般违规
			String yanzhongweigui = list.get(9).text();// 严重违规
			String shoujiaweigui = list.get(10).text();// 售假违规
			String zizhi = list.get(11).text();// 商城资质

			String gongsiType = list.get(12).text();// 公司类型
			String jingyingzhuangtai = list.get(13).text();// 经营状态
			String zhuceziben = list.get(14).text();// 注册资本

			String chenglinianfen = list.get(15).text();// 成立年份
			String gudongrenshu = list.get(16).text();// 股东人数
			String nashuoneixing = list.get(17).text();// 纳税类型
			String brandguishu = list.get(18).text();// 商标归属
			String brandguishuType = list.get(19).text();// 商标归属类型
			String brandliandai = list.get(20).text();// 商标是否连带
			String brandType = list.get(21).text();// 商标类型
			String brandzhonglei = list.get(22).text();// 商标种类
			String brandregisterTime = list.get(23).text();// 商标申请时间
			String yijingkaitongyijileimu = docCompanyDetailDocument.getElementsByClass("cont").get(0).text();// 已开通一级类目

			// System.out.println(yijingkaitongyijileimu);

			String sql = "insert into tianmao (companyName,saleprice,jishunianfei,xfzbzj,wangdianNum,maijiadesc,zhuangtai,comtype,shangchengtype,shangchengCategory,renqi,"
					+ "ruzhuTime,daikuan,daihuo,area,yibanweigui,yanzhongweigui,shoujiaweigui,zizhi,gongsiType,jingyingzhuangtai,zhuceziben,chenglinianfen,gudongrenshu,"
					+ "nashuoneixing,brandguishu,brandguishuType,brandliandai,brandType,brandzhonglei,brandregisterTime,yijingkaitongyijileimu)"
					+ "	 values ('" + companyName + "','" + saleprice + "','" + jishunianfei + "','" + xfzbzj + "','"
					+ wangdianNum + "','" + maijiadesc + "','" + zhuangtai + "','" + comtype + "','" + shangchengtype
					+ "','" + shangchengCategory + "','" + renqi + "','" + ruzhuTime + "','" + daikuan + "','" + daihuo
					+ "','" + area + "','" + yibanweigui + "','" + yanzhongweigui + "','" + shoujiaweigui + "','"
					+ zizhi + "','" + gongsiType + "','" + jingyingzhuangtai + "','" + zhuceziben + "','"
					+ chenglinianfen + "','" + gudongrenshu + "','" + nashuoneixing + "','" + brandguishu + "','"
					+ brandguishuType + "','" + brandliandai + "','" + brandType + "','" + brandzhonglei + "','"
					+ brandregisterTime + "','" + yijingkaitongyijileimu + "')";

			// System.out.println(sql);
			stmt.executeUpdate(sql);
			ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
			int id = 0;
			if (rs.next()) {
				id = rs.getInt(1);
			}
			Thread.sleep(1000);

		}
		conn.close();

	}

}
